package cn.broadsense.web.controller;

import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.constant.HttpStatus;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.enums.BusinessType;
import cn.broadsense.common.exception.file.InvalidExtensionException;
import cn.broadsense.common.utils.file.FileUploadUtils;
import cn.broadsense.common.utils.file.FileUtils;
import cn.broadsense.common.utils.file.ImageUtils;
import cn.broadsense.common.utils.file.MimeTypeUtils;
import cn.broadsense.web.controller.body.OCRBody;
import cn.broadsense.web.controller.vo.FileVO;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huangjinshi
 * @description 公共模块接口
 * @createDate 2023-09-12 17:29:41
 */
@Slf4j
@Api(tags = "公共模块")
@RestController
@RequestMapping("/api/v1/common")
public class CommonController {

    @Value("${spring.profiles.active}")
    private String env;

    @Value("${minio.url}")
    private String minioUrl;

    @Autowired
    private Tesseract tesseract;


    @ApiOperation("图片上传")
    @PostMapping("/upload")
    @Log(title = "公共模块", businessType = BusinessType.UPLOAD)
    public R<FileVO> uploadImage(MultipartFile file) throws InvalidExtensionException, IOException {
        try {
            if (file != null) {
                FileUploadUtils.assertAllowed(file, MimeTypeUtils.IMAGE_EXTENSION);
                String fileName = FileUploadUtils.uploadMinio(file);
                if (env != null && env.equalsIgnoreCase("prod")) {
                    //todo 后面需要修改，替换成正式地址
                    fileName = fileName.replace(minioUrl, "https://175.10.38.166:8086/v-tag");
                }
                FileVO data = new FileVO(fileName, FileUtils.getName(fileName), file.getOriginalFilename());
                return R.ok(data);
            } else {
                return R.fail(HttpStatus.BAD_REQUEST, "请上传图片");
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @GetMapping("/device/model")
    @ApiOperation("获取设备型号树")
    public R<List<JSONObject>> getDeviceModel() {
        List<JSONObject> data = new ArrayList<>();
        JSONObject parent = new JSONObject();
        parent.put("name", "父级型号1");

        JSONObject child1 = new JSONObject();
        child1.put("name", "子级型号1");
        JSONObject child11 = new JSONObject();
        child11.put("name", "孙级型号11");
        child11.put("children", new ArrayList<>());
        JSONObject child12 = new JSONObject();
        child12.put("name", "孙级型号12");
        child12.put("children", new ArrayList<>());
        List<JSONObject> children1 = new ArrayList<>();
        children1.add(child11);
        children1.add(child12);
        child1.put("children", children1);

        JSONObject child2 = new JSONObject();
        child2.put("name", "子级型号2");
        child2.put("children", new ArrayList<>());

        List<JSONObject> children = new ArrayList<>();
        children.add(child1);
        children.add(child2);
        parent.put("children", children);
        data.add(parent);
        return R.ok(data);
    }

    @PostMapping("/recognize")
    @ApiOperation("图像识别")
    public R<String> recognize(@RequestBody @Valid OCRBody body) throws TesseractException {
        // 获取Base64字符串
        String image = body.getImage();
        if (image.contains("data:")) {
            int start = image.indexOf(",");
            image = image.substring(start + 1);
        }
        BufferedImage bufferedImage = ImageUtils.base64ToBufferedImage(image);
        String content = tesseract.doOCR(bufferedImage);
        return R.ok(content);
    }

}
